

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blog/img/favicon.png">
  <link rel="icon" href="/blog/img/favicon.jpg">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
  <title>Sass-css拓展语言 - ITaylorfan的小站</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/blog/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.7.2/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/blog/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"itaylorfan.gitee.io","root":"/blog/","version":"1.8.11","typing":{"enable":true,"typeSpeed":70,"cursorChar":"❤","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/blog/js/utils.js" ></script>
  <script  src="/blog/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 85vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/blog/">&nbsp;<strong>ITaylorfan</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/blog/img/TS8-10-9.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="Sass-css拓展语言">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-11-17 08:36" pubdate>
        2021年11月17日 早上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.8k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      38
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
            <div class="scroll-down-bar">
              <i class="iconfont icon-arrowdown"></i>
            </div>
          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Sass-css拓展语言</h1>
            
            <div class="markdown-body">
              <blockquote>
<p>世界上最成熟、最稳定、最强大的专业级CSS扩展语言！</p>
</blockquote>
<p>目前主流的可编译CSS语言有以下几种</p>
<ol>
<li><code>less</code>，它是第一代编译型语言，功能强大，但是编译效率并不高，后期已经慢慢不使用了</li>
<li><code>sass</code>，它是第二代编译型语言，相较于less来说多了很多的功能，效率也很高，但是对于平台的依赖性比较强</li>
<li><code>stylus</code>，但简单，但是不友好 ，它的语言虽然简洁，但是书写起来容易让人产生不好的感觉</li>
</ol>
<p>上面的三种语言所实现的效果和功能都是一样的，它将我们原来的CSS语进行进行了编译型的扩展处理</p>
<p>SASS并不能直接运行在浏览器里面，它必须借助于第三方的平台，官方网站让我们使用<code>Ruby</code>去完成，但是我们不使用，因为我们有<code>node</code></p>
<p>前端使用的第三方编译包叫<code>node-sass</code></p>
<h2 id="安装node-sass运行环境"><a href="#安装node-sass运行环境" class="headerlink" title="安装node-sass运行环境"></a>安装<code>node-sass</code>运行环境</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs bash">$ npm init --yes<br>$ npm install node-sass --save-dev<br></code></pre></td></tr></table></figure>

<h2 id="配置node-sass的编译命令"><a href="#配置node-sass的编译命令" class="headerlink" title="配置node-sass的编译命令"></a>配置node-sass的编译命令</h2><p>在<code>package.json</code>的文件下面，我们要配置如下命令</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs javascript">node-sass --watch sass/<span class="hljs-number">01</span>.sass css/<span class="hljs-number">01</span>.css<br></code></pre></td></tr></table></figure>

<blockquote>
<p>–watch代表是监听那一个文件变化</p>
<p>css/01.css则代表把这个文件编译一以另一个地方</p>
</blockquote>
<p>我们在项目下面应该是有一个<code>sass</code>的文件夹，还有一个<code>css</code>的文件夹，我们要将<code>sass</code>文件夹下面的文件全部编译生CSS，放在<code>css</code>的目录下在，这个可以可以把上面的命令换一下</p>
<figure class="highlight ada"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs ada">node-sass -w &lt;源文件夹&gt; -o 目标文件夹<br>-w ，<span class="hljs-comment">--watch 监控某一个文件夹</span><br>-o ,<span class="hljs-comment">--output 编译输出到另一个文件夹</span><br>-r, <span class="hljs-comment">--recursive 递归，如果文件夹下面还有子文件夹，也进行相应的操作</span><br></code></pre></td></tr></table></figure>

<p>上面的命令可以直接变成下面的命令</p>
<figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs crmsh">$ build<span class="hljs-string">&quot;:&quot;</span><span class="hljs-keyword">node</span><span class="hljs-title">-sass</span> -w ./sass -o ./css<br></code></pre></td></tr></table></figure>

<p>为了文件操作，我们可以直接在<code>package.json</code>当中来配置这个命令</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs json">&#123;<br>    <span class="hljs-attr">&quot;name&quot;</span>: <span class="hljs-string">&quot;110501&quot;</span>,<br>    <span class="hljs-attr">&quot;version&quot;</span>: <span class="hljs-string">&quot;1.0.0&quot;</span>,<br>    <span class="hljs-attr">&quot;description&quot;</span>: <span class="hljs-string">&quot;&quot;</span>,<br>    <span class="hljs-attr">&quot;main&quot;</span>: <span class="hljs-string">&quot;index.js&quot;</span>,<br>    <span class="hljs-attr">&quot;scripts&quot;</span>: &#123;<br>        <span class="hljs-attr">&quot;test&quot;</span>: <span class="hljs-string">&quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;</span>,<br>        <span class="hljs-attr">&quot;build&quot;</span>:<span class="hljs-string">&quot;node-sass -w ./sass -o ./css&quot;</span><br><br>    &#125;,<br>    <span class="hljs-attr">&quot;keywords&quot;</span>: [],<br>    <span class="hljs-attr">&quot;author&quot;</span>: <span class="hljs-string">&quot;&quot;</span>,<br>    <span class="hljs-attr">&quot;license&quot;</span>: <span class="hljs-string">&quot;ISC&quot;</span>,<br>    <span class="hljs-attr">&quot;devDependencies&quot;</span>: &#123;<br>        <span class="hljs-attr">&quot;node-sass&quot;</span>: <span class="hljs-string">&quot;^6.0.1&quot;</span><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>这个时候我们只用在控制台执行<code>npm run build</code>就可以了</p>
<h2 id="使用SASS"><a href="#使用SASS" class="headerlink" title="使用SASS"></a>使用SASS</h2><p>SASS的语法比较特殊，如下所示</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-selector-class">.box1</span><br>    <span class="hljs-attribute">width</span>: <span class="hljs-number">100px</span><br>    height: <span class="hljs-number">1200px</span><br></code></pre></td></tr></table></figure>

<p>上面就是SASS的语法以，它没有括号，也没有分号结果，它就是最原始的SASS语言，它不能被浏览器直接执行，需要经过<code>node-sass</code>编译成<code>css</code>语言才可以</p>
<p>SASS的语法是非常严格的，不能使用分号，不能使用空格，不能使用花括号，必须严格遵守缩进，否则无效。这种语法给程序员的代码书写造成了极大的不方便，这个时候SASS就推出了种一种语法叫<code>SCSS</code></p>
<p><strong>什么是SCSS？</strong></p>
<blockquote>
<p>SASS是标准，SCSS是语法</p>
<p>SCSS本质上面就是<code>SASS+CSS</code>，它是使用CSS的语法去完成<code>SASS</code>的功能</p>
<p>我们可以认为<code>SCSS</code>说是换了另一种语法在写CSS，或者可以认为是CSS做了一次功能性的功能</p>
<p>主要扩展就以下四个基本核心点</p>
</blockquote>
<h2 id="SCSS的四个基本核心点"><a href="#SCSS的四个基本核心点" class="headerlink" title="SCSS的四个基本核心点"></a>SCSS的四个基本核心点</h2><blockquote>
<p>也称之为SASS的四个基本核心点</p>
</blockquote>
<ol>
<li>变量</li>
<li>嵌套</li>
<li>混合</li>
<li>继承</li>
</ol>
<p>所以我们可以认为<code>SCSS</code>就是在原来的基础语法上面扩展了上面的四个点</p>
<p><strong>sass语法</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs sass">.box1<br>    width: 100px<br>    height: 120px<br>    background-color: red<br>    text-align: center<br></code></pre></td></tr></table></figure>

<p><strong>scss语法</strong></p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-selector-class">.box1</span>&#123;<br>    <span class="hljs-attribute">width</span>:<span class="hljs-number">100px</span>;<br>    <span class="hljs-attribute">height</span>:<span class="hljs-number">120px</span>;<br>    <span class="hljs-attribute">background-color</span>:red;<br>    <span class="hljs-attribute">text-align</span>:center;<br>    <span class="hljs-keyword">@include</span> <br>    <span class="hljs-keyword">@extends</span><br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="Sass的基础语法功能"><a href="#Sass的基础语法功能" class="headerlink" title="Sass的基础语法功能"></a>Sass的基础语法功能</h2><h3 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h3><figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-comment">//这是Sass的注释 </span><br><span class="hljs-comment">/*</span><br><span class="hljs-comment">    这是原本的CSS注释 </span><br><span class="hljs-comment">*/</span><br></code></pre></td></tr></table></figure>

<p>在上面的代码里面<code>//</code>代表的是<code>sass</code>语法的注释 ，在最终生成的<code>css</code>是没有的，而<code>/**/</code>的注释是则css注释 ，编译 以后会被保留</p>
<h3 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h3><p>在sass里同我们也可以声明变量</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-comment">//变量</span><br><span class="hljs-variable">$color1</span>:red;<br><span class="hljs-selector-class">.div1</span>&#123;<br>    <span class="hljs-attribute">background-color</span>: <span class="hljs-variable">$color1</span>;<br>&#125;<br><br><span class="hljs-variable">$color1</span>:blue;<br><span class="hljs-selector-class">.div2</span>&#123;<br>    <span class="hljs-attribute">background-color</span>: <span class="hljs-variable">$color1</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>变量是可以进行多次定义的，后面的定义会默认覆盖掉前面的定义值</p>
<p><strong>并不是所有后面定义的变量都会覆盖前面定义的变量</strong></p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-variable">$w</span>:<span class="hljs-number">200px</span>;<br><span class="hljs-variable">$w</span>:<span class="hljs-number">100px</span> !default;		<span class="hljs-comment">//这里的变量加了一个default代表默认值</span><br><span class="hljs-selector-class">.box1</span>&#123;<br>    <span class="hljs-attribute">width</span>: <span class="hljs-variable">$w</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>默认值如果在其它地方被定义了，则会被覆盖掉</p>
<h3 id="嵌套"><a href="#嵌套" class="headerlink" title="嵌套"></a>嵌套</h3><figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-comment">//主要讲嵌套</span><br><span class="hljs-comment">//在以前的CSS里面，经常有子代和后代选择器</span><br><span class="hljs-selector-class">.div1</span> &#123;<br>    <span class="hljs-attribute">width</span>: <span class="hljs-number">100px</span>;<br>    <span class="hljs-selector-tag">a</span> &#123;<br>        <span class="hljs-attribute">color</span>: red;<br>    &#125;<br>    &gt;<span class="hljs-selector-tag">p</span> &#123;<br>        <span class="hljs-attribute">height</span>: <span class="hljs-number">100px</span>;<br>        <span class="hljs-selector-id">#abc</span> &#123;<br>            <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50%</span>;<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>经过编译</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs css"><span class="hljs-selector-class">.div1</span> &#123;<br>    <span class="hljs-attribute">width</span>: <span class="hljs-number">100px</span>;<br>&#125;<br><br><span class="hljs-selector-class">.div1</span> <span class="hljs-selector-tag">a</span> &#123;<br>    <span class="hljs-attribute">color</span>: red;<br>&#125;<br><br><span class="hljs-selector-class">.div1</span>&gt;<span class="hljs-selector-tag">p</span> &#123;<br>    <span class="hljs-attribute">height</span>: <span class="hljs-number">100px</span>;<br>&#125;<br><br><span class="hljs-selector-class">.div1</span>&gt;<span class="hljs-selector-tag">p</span> <span class="hljs-selector-id">#abc</span> &#123;<br>    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50%</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>父级选择标识符&amp;</strong></p>
<p>在SCSS中，有时候，我们需要在子级当中使用父级选择器，这个时候，我们就需要使用&amp;来选取父级选择器</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-selector-id">#abc</span> &#123;<br>    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50%</span>;<br>    &amp;<span class="hljs-selector-pseudo">:hover</span>&#123;<br>        <span class="hljs-attribute">color</span>: yellow;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>经过编译生成如下</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs css"><span class="hljs-selector-id">#abc</span>&#123;<br>    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50%</span>;<br>&#125;<br><span class="hljs-selector-id">#abc</span><span class="hljs-selector-pseudo">:hover</span>&#123;<br>    <span class="hljs-attribute">color</span>: yellow;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>在嵌套里面，它会自动的根据我们的嵌套关系来实现如下的功能</p>
<ol>
<li>后代选择器</li>
<li>子代选择器</li>
<li>相邻兄弟选择器</li>
<li>普通兄弟选择器</li>
</ol>
<h3 id="混合"><a href="#混合" class="headerlink" title="混合"></a>混合</h3><p>混合可以理解JavaScript里面的函数，定义了函数以后就可以调用函数，函数可以接收参数，也可以返回值</p>
<p>假设我们希望某一个页上面的<code>button</code>具备一个最基本的样式，然后在这个基本的样式上面去扩展新的新的样式</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-comment">//定义一个混合器</span><br><span class="hljs-keyword">@mixin</span> btn() &#123;<br>    <span class="hljs-attribute">min-width</span>: <span class="hljs-number">90px</span>;<br>    <span class="hljs-attribute">height</span>: <span class="hljs-number">35px</span>;<br>    <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid lightgray;<br>    <span class="hljs-attribute">text-align</span>: center;<br>    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">10px</span>;<br>&#125;<br><br><br><span class="hljs-selector-class">.btn-danger</span>&#123;<br>    <span class="hljs-attribute">background-color</span>: red;<br>    <span class="hljs-keyword">@include</span> btn();<br>&#125;<br></code></pre></td></tr></table></figure>

<p>最终生成的代码如下</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs css"><span class="hljs-selector-class">.btn-danger</span> &#123;<br>    <span class="hljs-attribute">background-color</span>: red;<br>    <span class="hljs-attribute">min-width</span>: <span class="hljs-number">90px</span>;<br>    <span class="hljs-attribute">height</span>: <span class="hljs-number">35px</span>;<br>    <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid lightgray;<br>    <span class="hljs-attribute">text-align</span>: center;<br>    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">10px</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>混合器是可以定义参数的</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-comment">//弹性盒子</span><br><span class="hljs-keyword">@mixin</span> flex-box(<span class="hljs-variable">$dir</span>:row) &#123;<br>    <span class="hljs-attribute">display</span>: flex;<br>    <span class="hljs-attribute">flex-direction</span>: <span class="hljs-variable">$dir</span>;<br>&#125;<br><br><span class="hljs-selector-class">.box1</span>&#123;<br>    <span class="hljs-attribute">width</span>: <span class="hljs-number">150px</span>;<br>    <span class="hljs-keyword">@include</span> flex-box();<br>&#125;<br><br><span class="hljs-selector-class">.box2</span>&#123;<br>    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;<br>    <span class="hljs-keyword">@include</span> flex-box(column);<br>    <span class="hljs-keyword">@include</span> btn();<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>混合里面如果没有参数，后面的小括号可以省略</p>
</blockquote>
<h3 id="继承"><a href="#继承" class="headerlink" title="继承"></a>继承</h3><p>sass里面的继承其实就是使用了分组选择器的功能来实现效果</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-comment">//继承</span><br><br><span class="hljs-selector-class">.box1</span> &#123;<br>    <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;<br>    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;<br>    <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid red;<br>&#125;<br><br><br><span class="hljs-selector-class">.box2</span> &#123;<br>    <span class="hljs-attribute">background-color</span>: blue;<br>    <span class="hljs-keyword">@extend</span> .box1;<br>&#125;<br><br><span class="hljs-selector-class">.box3</span>&#123;<br>    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50%</span>;<br>    <span class="hljs-keyword">@extend</span> .box2;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>最终生成的代码</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs css"><span class="hljs-selector-class">.box1</span>,<br><span class="hljs-selector-class">.box2</span>,<br><span class="hljs-selector-class">.box3</span> &#123;<br>    <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;<br>    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;<br>    <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid red;<br>&#125;<br><br><span class="hljs-selector-class">.box2</span>,<br><span class="hljs-selector-class">.box3</span> &#123;<br>    <span class="hljs-attribute">background-color</span>: blue;<br>&#125;<br><br><span class="hljs-selector-class">.box3</span> &#123;<br>    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50%</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="跳出"><a href="#跳出" class="headerlink" title="跳出"></a>跳出</h3><p>跳出主要针对的是嵌套，它使用<code>@at-root</code>来完成</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-selector-class">.box1</span> &#123;<br>    <span class="hljs-attribute">width</span>: <span class="hljs-number">100px</span>;<br>    <span class="hljs-selector-class">.abc</span> &#123;<br>        <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;<br>        <span class="hljs-comment">//在任何地方写@at-root都代表跳到最外层</span><br>        <span class="hljs-keyword">@at-root</span> &#123;<br>            <span class="hljs-selector-class">.def</span> &#123;<br>                <span class="hljs-attribute">color</span>: blue;<br>            &#125;<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>**注意：<code>@at-root</code>默认情况下是跳不出<code>@media</code>媒体查询的 **</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-keyword">@media</span> <span class="hljs-keyword">only</span> screen <span class="hljs-keyword">and</span> (<span class="hljs-attribute">max-width</span>:<span class="hljs-number">768px</span>) &#123;<br>    <span class="hljs-selector-class">.box1</span> &#123;<br>        <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;<br><br>        <span class="hljs-selector-class">.bbb</span> &#123;<br>            <span class="hljs-attribute">height</span>: <span class="hljs-number">100px</span>;<br><br>            <span class="hljs-keyword">@at-root</span> &#123;<br>                <span class="hljs-selector-class">.ccc</span> &#123;<br>                    <span class="hljs-attribute">color</span>: blue;<br>                &#125;<br>            &#125;<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>最终生成如下</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs css"><span class="hljs-keyword">@media</span> <span class="hljs-keyword">only</span> screen <span class="hljs-keyword">and</span> (<span class="hljs-attribute">max-width</span>: <span class="hljs-number">768px</span>) &#123;<br>    <span class="hljs-selector-class">.box1</span> &#123;<br>        <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;<br>    &#125;<br><br>    <span class="hljs-selector-class">.box1</span> <span class="hljs-selector-class">.bbb</span> &#123;<br>        <span class="hljs-attribute">height</span>: <span class="hljs-number">100px</span>;<br>    &#125;<br><br>    <span class="hljs-selector-class">.ccc</span> &#123;<br>        <span class="hljs-attribute">color</span>: blue;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>如果要跳出媒体查询，则要使用<code>@at-root(without:media)</code>来进行</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-keyword">@media</span> <span class="hljs-keyword">only</span> screen <span class="hljs-keyword">and</span> (<span class="hljs-attribute">max-width</span>:<span class="hljs-number">768px</span>) &#123;<br>    <span class="hljs-selector-class">.box1</span> &#123;<br>        <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;<br>        <span class="hljs-selector-class">.bbb</span> &#123;<br>            <span class="hljs-attribute">height</span>: <span class="hljs-number">100px</span>;<br>            <span class="hljs-keyword">@at-root</span>(<span class="hljs-attribute">without</span>:media) &#123;<br>                <span class="hljs-selector-class">.ccc</span> &#123;<br>                    <span class="hljs-attribute">color</span>: blue;<br>                &#125;<br>            &#125;<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="数组的定义"><a href="#数组的定义" class="headerlink" title="数组的定义"></a>数组的定义</h3><figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-comment">//数组的定义</span><br><span class="hljs-variable">$a</span>:red; <span class="hljs-comment">//定久了一个变量</span><br><br><span class="hljs-variable">$list</span>:red,blue,yellow,pink;<br></code></pre></td></tr></table></figure>

<p>一般情况下，为了便于更好的理解与格式化，我们都会用小括号将值包含起来，如下</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-variable">$list</span>:(red,blue,yellow,pink);<br></code></pre></td></tr></table></figure>

<h3 id="对象的定义"><a href="#对象的定义" class="headerlink" title="对象的定义"></a>对象的定义</h3><figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-comment">//对象的定久【键值对的定义】</span><br><span class="hljs-variable">$themeColor</span>:(<br>    primary:lightseagreen,<br>    danger:red,<br>    warning:orange,<br>    success:green<br>);<br><br><span class="hljs-keyword">@each</span> <span class="hljs-variable">$key</span>,<span class="hljs-variable">$value</span> in <span class="hljs-variable">$themeColor</span> &#123;<br>    <span class="hljs-selector-class">.bg-</span>#&#123;<span class="hljs-variable">$key</span>&#125;&#123;<br>        <span class="hljs-attribute">background-color</span>: <span class="hljs-variable">$value</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>上面就是键值对的定义及键值对的遍历过程，最终生成的代码如下</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-selector-class">.bg-primary</span> &#123;<br>    <span class="hljs-attribute">background-color</span>: lightseagreen;<br>&#125;<br><br><span class="hljs-selector-class">.bg-danger</span> &#123;<br>    <span class="hljs-attribute">background-color</span>: red;<br>&#125;<br><br><span class="hljs-selector-class">.bg-warning</span> &#123;<br>    <span class="hljs-attribute">background-color</span>: orange;<br>&#125;<br><br><span class="hljs-selector-class">.bg-success</span> &#123;<br>    <span class="hljs-attribute">background-color</span>: green;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="set的定义"><a href="#set的定义" class="headerlink" title="set的定义"></a>set的定义</h3><p>之前数组的定义就是set的定义</p>
<h3 id="map的定义"><a href="#map的定义" class="headerlink" title="map的定义"></a>map的定义</h3><p>之前对象的定义就是map的定义</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-variable">$themeColor</span>:(primary:lightseagreen,<br>    danger:red,<br>    warning:orange,<br>    success:green);<br><span class="hljs-selector-class">.box1</span> &#123;<br>    <span class="hljs-attribute">width</span>: <span class="hljs-number">100px</span>;<br>    <span class="hljs-comment">//我想color使用主色调primary</span><br>    <span class="hljs-comment">//通过key找某一个值 ，怎么办</span><br>    <span class="hljs-attribute">color</span>: map-get(<span class="hljs-variable">$themeColor</span>, primary);<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="循环"><a href="#循环" class="headerlink" title="循环"></a>循环</h3><p>循环是所有编程语言最基本的点，在<code>sass</code>里面，也可以使循环，它使用<code>@for</code>来开始，语法格式如下</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-keyword">@for</span> <span class="hljs-variable">$i</span> from start through end &#123;<br>    <br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>start与end分别代表开始与结束</p>
</blockquote>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-keyword">@for</span> <span class="hljs-variable">$i</span> from start to end &#123;<br>    <br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>在上面的2个语法里面，一个是<code>through</code>关键字，一个是<code>to</code>的关键字</p>
<p><code>through</code>是穿过的意思，它包含end,而<code>to</code>则不包含end</p>
</blockquote>
<p>如果我们想生成下面的css代码，怎么写呢</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs css"><span class="hljs-selector-class">.div1</span> &#123;<br>    <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">&quot;./img/1.jpg&quot;</span>);<br>&#125;<br><span class="hljs-selector-class">.div2</span> &#123;<br>    <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">&quot;./img/2.jpg&quot;</span>);<br>&#125;<br><span class="hljs-selector-class">.div3</span> &#123;<br>    <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">&quot;./img/3.jpg&quot;</span>);<br><br>&#125;<br><span class="hljs-selector-class">.div4</span> &#123;<br>    <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">&quot;./img/4.jpg&quot;</span>);<br>&#125;<br><span class="hljs-selector-class">.div5</span> &#123;<br>    <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">&quot;./img/5.jpg&quot;</span>);<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>SCSS代码如下</strong></p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-comment">//遍历，循环</span><br><span class="hljs-keyword">@for</span> <span class="hljs-variable">$i</span> from <span class="hljs-number">1</span> through <span class="hljs-number">5</span> &#123;<br>    <span class="hljs-selector-class">.div</span>#&#123;<span class="hljs-variable">$i</span>&#125;&#123;<br>        <span class="hljs-attribute">background-image</span>: url(<span class="hljs-string">&quot;./img/#&#123;$i&#125;.jpg&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>在上面的代码里面要注意2点</p>
<ol>
<li>through与to的区别</li>
<li>变量插值使用#{}来完成</li>
</ol>
</blockquote>
<h3 id="遍历"><a href="#遍历" class="headerlink" title="遍历"></a>遍历</h3><figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-comment">//数组</span><br><span class="hljs-variable">$arr</span>:(red, blue, yellow, pink);<br><br><span class="hljs-keyword">@each</span> <span class="hljs-variable">$item</span> in <span class="hljs-variable">$arr</span> &#123;<br>    <span class="hljs-selector-class">.bg-</span>#&#123;<span class="hljs-variable">$item</span>&#125; &#123;<br>        <span class="hljs-attribute">background-color</span>: <span class="hljs-variable">$item</span>;<br>    &#125;<br>&#125;<br><br><span class="hljs-variable">$index</span>:<span class="hljs-number">1</span>;<br><br><span class="hljs-keyword">@each</span> <span class="hljs-variable">$item</span> in <span class="hljs-variable">$arr</span> &#123;<br>    <span class="hljs-selector-class">.bg-</span>#&#123;<span class="hljs-variable">$index</span>&#125; &#123;<br>        <span class="hljs-attribute">background-color</span>: <span class="hljs-variable">$item</span>;<br>    &#125;<br>    <span class="hljs-variable">$index</span>:<span class="hljs-variable">$index</span> + <span class="hljs-number">1</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>上面的代码最终生成如下形式</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs css"><span class="hljs-selector-class">.bg-red</span> &#123;<br>    <span class="hljs-attribute">background-color</span>: red;<br>&#125;<br><br><span class="hljs-selector-class">.bg-blue</span> &#123;<br>    <span class="hljs-attribute">background-color</span>: blue;<br>&#125;<br><br><span class="hljs-selector-class">.bg-yellow</span> &#123;<br>    <span class="hljs-attribute">background-color</span>: yellow;<br>&#125;<br><br><span class="hljs-selector-class">.bg-pink</span> &#123;<br>    <span class="hljs-attribute">background-color</span>: pink;<br>&#125;<br><br><span class="hljs-selector-class">.bg-1</span> &#123;<br>    <span class="hljs-attribute">background-color</span>: red;<br>&#125;<br><br><span class="hljs-selector-class">.bg-2</span> &#123;<br>    <span class="hljs-attribute">background-color</span>: blue;<br>&#125;<br><br><span class="hljs-selector-class">.bg-3</span> &#123;<br>    <span class="hljs-attribute">background-color</span>: yellow;<br>&#125;<br><br><span class="hljs-selector-class">.bg-4</span> &#123;<br>    <span class="hljs-attribute">background-color</span>: pink;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="条件判断"><a href="#条件判断" class="headerlink" title="条件判断"></a>条件判断</h3><p>在<code>sass</code>里面实现条件判断有两种语法</p>
<ol>
<li>三目运算符（三元表达式，条件表达式）</li>
<li>@if语句</li>
</ol>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-comment">//条件判断</span><br><span class="hljs-variable">$w</span>:<span class="hljs-number">100px</span>;<br><span class="hljs-selector-class">.box1</span> &#123;<br>    <span class="hljs-attribute">color</span>: red;<br>    <span class="hljs-comment">//如果$w&gt;200px则背景为蓝色，否则为黑色</span><br>    <span class="hljs-keyword">@if</span> <span class="hljs-variable">$w</span>&gt;<span class="hljs-number">200px</span> &#123;<br>        <span class="hljs-attribute">background-color</span>: blue;<br>    &#125;<br><br>    <span class="hljs-keyword">@else</span> &#123;<br>        <span class="hljs-attribute">background-color</span>: black;<br>    &#125;<br>&#125;<br><br><span class="hljs-variable">$index</span>:<span class="hljs-number">4</span>;<br><span class="hljs-selector-class">.box</span> &#123;<br>    <span class="hljs-attribute">width</span>: <span class="hljs-number">100px</span>;<br>    <span class="hljs-comment">//如果index为1，颜色为红色，如果index为2，背景为蓝色，否则背景为黑色</span><br>    <span class="hljs-keyword">@if</span> <span class="hljs-variable">$index</span>==<span class="hljs-number">1</span> &#123;<br>        <span class="hljs-attribute">background-color</span>: red;<br>    &#125;<br>    <span class="hljs-keyword">@else</span> if <span class="hljs-variable">$index</span>==<span class="hljs-number">2</span> &#123;<br>        <span class="hljs-attribute">background-color</span>: blue;<br>    &#125;<br><br>    <span class="hljs-keyword">@else</span> &#123;<br>        <span class="hljs-attribute">background-color</span>: black;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>上面是条件判断里面的if语句,还可以使用if函数来进行</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-variable">$a</span>:<span class="hljs-number">2</span>;<br><span class="hljs-selector-class">.div4</span> &#123;<br>    <span class="hljs-attribute">color</span>: red;<br>    <span class="hljs-attribute">background-color</span>: if(<span class="hljs-variable">$a</span>==<span class="hljs-number">1</span>, rgb(<span class="hljs-number">139</span>, <span class="hljs-number">136</span>, <span class="hljs-number">136</span>), blue);<br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h3><p>这个点与我们之前所学习的点是一样的，它可以使用<code>@function</code>的关键字来定义函数，也可以使用<code>@return</code>来返回值</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-variable">$baseFontSize</span>: <span class="hljs-number">16px</span> !default;<br><span class="hljs-keyword">@function</span> pxToRem(<span class="hljs-variable">$px</span>)&#123;<br>    <span class="hljs-keyword">@return</span> <span class="hljs-variable">$px</span> / <span class="hljs-variable">$baseFontSize</span> * <span class="hljs-number">1rem</span>;<br>&#125;<br><span class="hljs-selector-class">.box2</span>&#123;<br>    <span class="hljs-attribute">width</span>: pxToRem(<span class="hljs-number">100px</span>);<br>&#125;<br></code></pre></td></tr></table></figure>

<p>scss里面有很多自带的函数</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-selector-class">.box2</span> &#123;<br>    <span class="hljs-attribute">width</span>: pxToRem(<span class="hljs-number">100px</span>);<br>    <span class="hljs-attribute">color</span>: lighten(<span class="hljs-number">#000000</span>, <span class="hljs-number">10%</span>);<br>    <span class="hljs-attribute">background-color</span>: darken(red, <span class="hljs-number">10%</span>);<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="运算"><a href="#运算" class="headerlink" title="运算"></a>运算</h3><blockquote>
<p>sass具有运算的特性，可以对数值型的Value(如：数字、颜色、变量等)进行加减乘除四则运算。请注意运算符前后请留一个空格，不然会出错。</p>
</blockquote>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs scss"><span class="hljs-variable">$baseFontSize</span>:          <span class="hljs-number">14px</span> !default;<br><span class="hljs-variable">$baseLineHeight</span>:        <span class="hljs-number">1.5</span> !default;<br><span class="hljs-variable">$baseGap</span>:               <span class="hljs-variable">$baseFontSize</span> * <span class="hljs-variable">$baseLineHeight</span> !default;<br><span class="hljs-variable">$halfBaseGap</span>:           <span class="hljs-variable">$baseGap</span> / <span class="hljs-number">2</span>  !default;<br><span class="hljs-variable">$samllFontSize</span>:         <span class="hljs-variable">$baseFontSize</span> - <span class="hljs-number">2px</span>  !default;<br></code></pre></td></tr></table></figure>


            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/blog/categories/%E5%89%8D%E7%AB%AF%E7%AC%94%E8%AE%B0/">前端笔记</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/blog/tags/CSS/">CSS</a>
                    
                      <a class="hover-with-bg" href="/blog/tags/CSS3/">CSS3</a>
                    
                      <a class="hover-with-bg" href="/blog/tags/Sass/">Sass</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/blog/2021/11/30/%E5%89%8D%E7%AB%AF%E8%BF%9B%E9%98%B6-Vue%E5%9F%BA%E7%A1%80/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Vue基础</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/blog/2021/11/16/Node.js%E7%AC%94%E8%AE%B0/">
                        <span class="hidden-mobile">Node.js笔记</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" ></script>
<script  src="/blog/js/events.js" ></script>
<script  src="/blog/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/blog/js/img-lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.3/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.1/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js" ></script>



  <script  src="/blog/js/local-search.js" ></script>






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.12/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>









  <script  src="https://cdn.jsdelivr.net/npm/mermaid@8.10.1/dist/mermaid.min.js" ></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({"theme":"default"});
    }
  </script>







<!-- 主题的启动项 保持在最底部 -->
<script  src="/blog/js/boot.js" ></script>


</body>
</html>
